package bussinessService.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import module.Shop;
import domain.vo.ShopVO;
import domain.vo.queryObject;
import bussinessService.shopBussinessService;

/**
 * 
* @ClassName: ShopBussinessServiceImpl 
* @Description:门店业务层实现
* @author chenchao
* @date 2014年1月16日 下午2:42:29 
*
 */
public class ShopBussinessServiceImpl  implements shopBussinessService{


	public Shop getById(String id) throws SQLException {
		// TODO Auto-generated method stub
		return shopService.getById(id);
	}


	public List<ShopVO> getAll() throws SQLException {
		List<ShopVO> vos = new ArrayList<ShopVO>();
		List<Shop> results = new ArrayList<Shop>();
		results = shopService.getAll();
		ShopVO tv = new ShopVO();
		tv.setShopId(-1);
		tv.setShopName("全部门店");
		tv.setSelected(true);
		vos.add(tv);
		for(Shop s:results) {
			if (s.getParentShopId() != -1) {
				ShopVO vo = new ShopVO();
				vo.setShopId(s.getShopId());
				vo.setShopName(s.getShopName());
				vo.setParentShopId(s.getParentShopId());
				vos.add(vo);
			}
		}
		return vos;
	}

	
	public void merageObject(Shop t) throws SQLException {
		if (t.getShopId() == 0) {
			shopService.insertObject(t);
		} else {
			shopService.updateObject(t);
		}
		
	}


	public void deleteT(int id) throws SQLException {
		shopService.deleteT(id);
		
	}


	public List<ShopVO> getObjectByQueryObject(queryObject queryObject)
			throws SQLException {
		List<ShopVO> vos = new ArrayList<ShopVO>();
		List<Shop> results = new ArrayList<Shop>();
		results = shopService.getObjectByQueryObject(queryObject);
		//parent Nodes
		for(Shop s:results) {
			if (s.getParentShopId() == -1) {
				ShopVO vo = new ShopVO();
				vo.setId(s.getShopId());
				vo.setText(s.getShopName());
				vo.setState("open");
				vo.setShopId(s.getShopId());
				vo.setShopName(s.getShopName());
				vos.add(vo);
			}
		}
		//Child Nodes
		for(ShopVO s:vos) {
			for(Shop ts:results) {
				if(ts.getParentShopId() == s.getId()) {
					ShopVO cvo = new ShopVO();
					cvo.setId(ts.getShopId());
					cvo.setText(ts.getShopName());
					s.getChildren().add(cvo);
				}
			}
		}
		return vos;
	}

	
	public int countByQueryObject(queryObject queryObject) throws SQLException {

		return shopService.countByQueryObject(queryObject);
	}

}
